#include <QtCore/QCoreApplication>
#include <QtDebug>
#include <QtSql/QtSql>

int main(int argc, char *argv[]) {

    QCoreApplication a(argc, argv);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL3");
    db.setConnectOptions();
    db.setHostName("localhost");
    db.setDatabaseName("mydb");
    db.setUserName("root");
    db.setPassword("Gabriel1!");
    db.setPort(3306);

    if(db.open()) {

        qDebug() << "Opened!";
        QSqlQuery qry;

        if(qry.exec("SELECT * FROM fruits;")) {
            while(qry.next()) {
            qDebug() << qry.value(0).toString();
            }
        }
        else {
        qDebug() << db.lastError().text();
        }

        qDebug() << "Closing…";
        db.close();
    }
    else {
    qDebug() << "Something went Wrong:" << db.lastError().text();
    }
 

    qDebug() << "Exiting program";

    return a.exec();

}
